﻿using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Cache
{
    public class ProjectCache
    {
        /// <summary>
        /// 互斥锁
        /// </summary>
        private static object lockHelper = new object();
        #region 车辆基本信息

        private static Dictionary<int, CarInfo> carInfo = null;

        public static Dictionary<int, CarInfo> CarInfo
        {
            get
            {
                if (carInfo == null)
                {
                    lock (lockHelper)
                    {
                        if (carInfo == null)
                        {
                            carInfo = new Dictionary<int, CarInfo>();
                            CarInfoDAO dal = new CarInfoDAO();
                            var list = dal.GetList(string.Empty);
                            foreach (var ent in list)
                            {
                                carInfo.Add(ent.Id, ent);
                            }
                        }
                    }
                }
                return carInfo;
            }
        }
        public static void ClearCardInfo()
        {
            carInfo = null;
        }
        #endregion

        #region 用户手机编码
        private static Dictionary<int, UserToken> userToken = null;
        public static Dictionary<int, UserToken> UserToken
        {
            get
            {
                if (userToken == null)
                {
                    userToken = new Dictionary<int, UserToken>();
                    UserTokenDAO dal = new UserTokenDAO();
                    var list = dal.GetList();
                    foreach (var ent in list)
                    {
                        userToken.Add(ent.UserId, ent);
                    }
                }
                return userToken;
            }
        }
        public static void ClearUserToken()
        {
            userToken = null;
        }
        #endregion

        #region 基本信息大类
        private static Dictionary<int, BaseItemType> baseItemType = null;
        public static Dictionary<int, BaseItemType> BaseItemType
        {
            get
            {
                if (baseItemType == null)
                {
                    lock (lockHelper)
                    {
                        var list = new BaseItemTypeDAO().GetList();
                        baseItemType = new Dictionary<int, BaseItemType>();
                        foreach (var item in list)
                        {
                            baseItemType.Add(item.Id, item);
                        }
                    }
                }
                return baseItemType;
            }
        }
        public static void ClearBaseItemType()
        {
            baseItemType = null;
        }
        #endregion

        #region 基本信息明细
        private static Dictionary<int, BaseItem> baseItem = null;
        public static Dictionary<int, BaseItem> BaseItem
        {
            get
            {
                if (baseItem == null)
                {
                    lock (lockHelper)
                    {
                        baseItem = new Dictionary<int, Domain.BaseItem>();
                        var list = new BaseItemDAO().GetList();
                        foreach (var item in list)
                        {
                            baseItem.Add(item.Id, item);
                        }
                    }
                }
                return baseItem;
            }
        }
        public static void ClearBaseItem()
        {
            baseItem = null;
        }
        #endregion
    }
}
